package com.yejiali.backend.utils;

import com.yejiali.backend.entity.MyUserDetails;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;


/**
 * @Author:yejiali
 * @Date:2025/2/26-02-26-10:58
 * @Decsription:org.yejiali.util
 * @Version:1.0
 */
@Component
public class SecurityUtil {
    /**
     * 获取用户
     **/
    public static MyUserDetails getUserDetails() {
        return (MyUserDetails) getAuthentication().getPrincipal();
    }

    /**
     * 获取Authentication
     */
    public static Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    public static Boolean isAdmin() {
        Long id = getUserDetails().getUser().getId();
        return id != null && 1L == id;
    }

    public static Long getUserId() {
        return getUserDetails().getUser().getId();
    }
}

